import Vue from "vue";
import VueRouter from "vue-router";
import Index from "../views/IndexView.vue";
import Login from "../views/LoginView.vue";
import Home from "../views/HomeView.vue";
import User from "../views/user/UserView.vue";
import Rights from "../views/power/RightsView.vue";
import Roles from "../views/power/RolesView.vue";
import HomeIndex from "../views/home/IndexView.vue";
import Goods from "../views/goods/GoodsView.vue";
import Categories from "../views/goods/CategoriesView.vue";
import Params from "../views/goods/ParamsView.vue";
import AddGood from "../views/goods/AddGood.vue";
import Orders from "../views/orders/OrdersView.vue";
import Reports from "../views/reports/ReportsView.vue";
// 学router为了就是让所有的页面,在同一页面内显示,SPA单页面应用
Vue.use(VueRouter);
// 路由的路径
const routes = [
  { path: "/", name: "index", component: Index },
  { path: "/login", name: "login", component: Login },
  // react 会直接的拼起来前边的路径
  {
    path: "/home",
    name: "home",
    component: Home,
    redirect: "/homeIndex",
    /* vue二级的路由不会自动的拼接前边的 ctrl+shift+y小写 x大写 */
    children: [
      { path: "/homeIndex", name: "homeIndex", component: HomeIndex },
      { path: "/users", name: "users", component: User },
      { path: "/rights", name: "rights", component: Rights },
      { path: "/roles", name: "roles", component: Roles },
      { path: "/goods", name: "goods", component: Goods },
      { path: "/categories", name: "categories", component: Categories },
      { path: "/params", name: "params", component: Params },
      { path: "/goods/add", name: "AddGood", component: AddGood },
      { path: "/orders", name: "orders", component: Orders },
      { path: "/reports", name: "reports", component: Reports },
    ],
  },
];

const router = new VueRouter({
  routes,
});
/* beforeEach导航守卫后台的页面或者是功能,不能随意访问,to 去按个路径 form 从哪个路径来 next  */
router.beforeEach((to, form, next) => {
  // debugger||逻辑或,只要前边的为true
  if (to.path === "/" || to.path === "/login") {
    // 让路由继续往下走
    return next();
  }
  const token = window.sessionStorage.getItem("token");
  // console.log(to.path)
  // 保存后台的path
  window.sessionStorage.setItem("path", to.path)
  // token没值,没有登陆过
  if (!token) {
    console.log("不能放行,请登录去");
    return next("/login");
  }
  // 让路由继续往下走
  return next();
});
export default router;
